// 配置信息

const fs = require('fs')
const path = require('path')
const multer = require('koa-multer') // 上传模块
const { nowDate } = require('./index')

const secret = "jdf-jiubugaosuni-jwt" // jwt鉴权

// 图片上传配置
const IMAGE_CONFIG = multer.diskStorage({
    // 图片储存位置
    destination: (req, file, cb) => {
        let time = nowDate(3)
        let dir = path.join("./public/uploads/" + time)

        if(!fs.existsSync(dir)) {
            fs.mkdirSync(dir, { recursive: true })
        }

        cb(null, dir)
    },
    // 设置图片名字
    filename: (req, file, cb) => {
        let fileName = nowDate() + path.extname(file.originalname)

        cb(null, fileName)
    }
})

// 图片上传限制
const IMAGE_FILTER = (req, file, cb) => {
    let type = file.originalname.split('.').splice(-1)

    if (type == 'jpeg' || type == 'jpg') {
        cb(null, true)
    } else {
        cb(null, false)
    }
}

module.exports = {
    secret,
    IMAGE_CONFIG,
    IMAGE_FILTER
}
